package enron;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.Action;
import javax.swing.Icon;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class CheckBoxRenderer extends JCheckBox implements TableCellRenderer {

	private static final long serialVersionUID = 287460621212992467L;
	private TableCellRenderer delegate;
	private JCheckBox checkBox;
	private boolean defaultValue = false;

	public CheckBoxRenderer(String string, TableCellRenderer defaultRenderer, boolean defaultValue) {
		checkBox = new JCheckBox(string);
		delegate = defaultRenderer;
		this.defaultValue = defaultValue;
		setName("Table.checkBoxCellRenderer");
		setLayout(new BorderLayout());
		checkBox.setBorder(new EmptyBorder(0,0,0,0));
		//disable component background painting
		checkBox.setOpaque(false);
		//checkBox.get
		add(checkBox);
	}

	public Component getTableCellRendererComponent(
			JTable table, Object value, boolean isSelected,
			boolean hasFocus, int row, int column) {
		JComponent c;
		if(value != null && Appliance.class.isInstance(value)) {
			c = (JComponent)delegate.getTableCellRendererComponent(table, 
					((Appliance)value).isTurnedOn(), isSelected, hasFocus, row, column);
		} else {
			c = (JComponent)delegate.getTableCellRendererComponent(table, value, 
					isSelected, hasFocus, row, column);
		}
		//respect insets and focus
		setBorder(c.getBorder());
		//copy opacity
		setOpaque(c.isOpaque());
		//apply background
		setBackground(c.getBackground());    
		//apply foreground
		checkBox.setForeground(c.getForeground());
		if(value == null) {
			checkBox.setSelected(defaultValue);
		} else if(Boolean.class.isInstance(value)) {
			checkBox.setSelected((Boolean) value);
		} else if(Appliance.class.isInstance(value)) {
			//checkBox.putClientProperty("enron.Appliance",(Appliance)value);
			checkBox.setText(((Appliance)value).getDisplay());
			checkBox.setSelected(((Appliance)value).isTurnedOn());
		}
		return this;
	}
	
	public CheckBoxRenderer() {
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(Icon icon) {
		super(icon);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(String text) {
		super(text);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(Action a) {
		super(a);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(Icon icon, boolean selected) {
		super(icon, selected);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(String text, boolean selected) {
		super(text, selected);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(String text, Icon icon) {
		super(text, icon);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(String text, Icon icon, boolean selected) {
		super(text, icon, selected);
		// TODO Auto-generated constructor stub
	}

	public CheckBoxRenderer(TableCellRenderer defaultRenderer) {
		checkBox = new JCheckBox();
		delegate = defaultRenderer;

		setName("Table.checkBoxCellRenderer");
		setLayout(new BorderLayout());
		checkBox.setBorder(new EmptyBorder(0,0,0,0));
		//disable component background painting
		checkBox.setOpaque(false);
		//checkBox.get
		add(checkBox);
	}
}
